package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyContent;
import cn.wolfcode.trip.base.mapper.DailyContentMapper;
import cn.wolfcode.trip.base.mapper.DailyMapper;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DailyServiceImpl implements IDailyService {
    //内容
    @Autowired
    private DailyMapper dailyMapper;

    @Autowired
    private DailyContentMapper dailyContentMapper;

    //分页
    public PageInfo<Daily> query(DailyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Daily> list = dailyMapper.selectForList(qo);
        return new PageInfo(list);
    }

    //查询操作
    public Daily get(Long strategyId) {

        return dailyMapper.selectByPrimaryKey(strategyId);
    }

    public DailyContent getContentById(Long id) {
        return dailyContentMapper.selectByPrimaryKey(id);
    }

    //查询所有
    public List<Daily> listAll() {
        return dailyMapper.selectAll();
    }


    //新增或修改
    public void saveOrUpdate(Daily entity) {
        // 判断是否是发布状态,是就设置发布时间
        if (entity.getState() == Daily.STATE_RELEASE) {
            entity.setReleaseTime(new Date());
        }

        // 获取更新内容
        DailyContent content = entity.getDailyContent();
        if (entity.getId() != null) {

            //更新日报内容
            content.setId(entity.getId());
            content.setContent(entity.getDailyContent().getContent());
            //修改的时候把修改时间加上
            entity.setCreateTime(new Date());
            dailyMapper.updateByPrimaryKey(entity);
            dailyContentMapper.updateByPrimaryKey(content);
        } else {
            entity.setCreateTime(new Date());
            dailyMapper.insert(entity);

            content.setId(entity.getId());
            dailyContentMapper.insert(content);
        }

    }

}
